using Microsoft.AspNetCore.Mvc;
using ToDoListWeb.Models;
using ToDoListWeb.Service;
using Microsoft.AspNetCore.Http;

namespace ToDoListWeb.Controllers
{
    public class UserController : Controller
    {
        UserService _userService = new UserService();
        MatterService _matter = new MatterService();
        [HttpGet]
        public IActionResult Index()
        {
            return View("Login");
        }
        /// <summary>
        /// 界面：浏览所有清单、事项
        /// </summary>
        /// <returns></returns>
        public IActionResult ShowEvents()
        {
            _userService.UpdateUserModel(GetUser());
            return View(GetUser());
        }
        /// <summary>
        /// 在布局页中显示用户昵称等信息，若昵称不存在，
        /// 则显示用户名
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetMenuInfo()
        { 
            _userService.UpdateUserModel(GetUser());
            var menuModel = _userService.GetMenuInfoModel(GetUser());
            return View(menuModel);
        }
        /// <summary>
        /// 用户界面：显示用户信息及更改用户昵称
        /// </summary>
        /// <returns></returns>
        public IActionResult UserInfo()
        {
            _userService.UpdateUserModel(GetUser());
            return View(GetUser());
        }
        /// <summary>
        /// 修改昵称
        /// </summary>
        /// <param name="nickName"></param>
        /// <returns></returns>
        public IActionResult ChangeNickName(string nickName)
        {
            if (nickName != null) {
                var user = GetUser();
                _userService.ChangeNickName(user.UserId,nickName);
            } 
            return View("ShowEvents", GetUser());
        }
        [HttpGet]
        /// <summary>
        /// 注销用户
        /// </summary>
        /// <returns></returns>
        public IActionResult Logout()
        {
            HttpContext.Session.Remove("user");
            return View("Login");
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Login(string userName, string password)
        {
            int userId = _userService.UserLogin(userName, password);
            if (userId != 0)
            {
                HttpContext.Session.SetInt32("user", userId);
                return View("ShowEvents", GetUser());
            }
            else
            {
                ViewData["Message"] = "你的用户名或密码错误，登录失败";
                return View();
            }
        }

        /// <summary>
        /// 注册界面
        /// </summary>
        /// <returns></returns>
        public IActionResult Register()
        {
            return View("Register");
        }
        /// <summary>
        ///注册用户 
        /// </summary>
        /// <param name="newUserName"></param>
        /// <param name="password"></param>
        /// <param name="rePassword"></param>
        /// <param name="nickName"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult RegisterUser(string newUserName, string password, string rePassword, string nickName)
        {
            //判断用户名是否存在
            var judgeUserName = _userService.UserRegister(newUserName, password, nickName);
           // 判断两次输入的密码是否一致
            if (password.Equals(rePassword)) {
                if (judgeUserName)
                {
                    //注册成功返回登录页面
                    _userService.UserRegister(newUserName, password, nickName);
                    return View("Login");     
                }
                else
                  ViewData["Message"] = "你输入用户名已存在,注册失败";

            }
            else
                ViewData["Message"] = "你输入的密码不一致,注册失败";
            return View("Register");

            //if (password.Equals(rePassword))
            //{
            //    //注册成功返回登录页面
            //    if (_userService.UserRegister(newUserName, password, nickName))
            //        return View("Login");
            //}

            //if (password == null || newUserName == null || rePassword == null)
            //{
            //    ViewData["Message"] = "输入不能为空";
            //    return View("Register");
            //}
            //else if (password.Equals(rePassword))
            //{
            //    //注册成功返回登录页面
            //    if (_userService.UserRegister(newUserName, password, nickName))
            //        return View("Login");
            //    else
            //    {
            //        ViewData["Message"] = "你输入用户名已存在";
            //        return View("Register");
            //    }
            //}
            //else
            //{
            //    ViewData["Message"] = "你输入的两次密码不一致";
            //    return View("Register");
            //}
        }
        /// <summary>
        /// 得到用户
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public UserModel GetUser() {
            var user = _userService.GetUserModel((int)(HttpContext.Session.GetInt32("user")));
            return user;
        }
    }
}